package 学生管理系统;

import java.awt.*;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;

public class app {
    public static void main(String[] args) {
        ArrayList<User> userList = new ArrayList<User>();
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出系统 ");
            System.out.println("请输入你的操作:");
            Scanner sc1 = new Scanner(System.in);
            String choose = sc1.next();
            switch (choose) {
                case "1" -> login(userList);//alt+回车
                case "2" -> register(userList);
                case "3" -> forgetPassword(userList);
                case "4" -> {
                    System.out.println("欢迎下次使用");
                    System.exit(0);
                }
                default -> System.out.println("您的输入有误");
            }
        }
    }

    private static void forgetPassword(ArrayList<User> list1) {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入你的用户名");
        String uname = sc1.next();
        boolean flag = contains(list1, uname);
        if (!flag) {
            System.out.println("此用户未注册");
            return;
        }
        System.out.println("请输入" + uname + "的身份证号码");
        String personalid = sc1.next();
        System.out.println("请输入" + uname + "的手机号码");
        String number = sc1.next();

        int index = getIndex(list1, uname);
        User u1 = list1.get(index);
        if (!(u1.getPersonalID().equalsIgnoreCase(personalid) && u1.getPhoneNumber().equals(number))) {
            System.out.println("身份证或手机号码不一致请重新输入");
            return;
        }

        while (true) {
            System.out.println("验证成功请输入新密码");
            String password = sc1.next();
            System.out.println("请再次输入新密码");
            String againpassword = sc1.next();
            if (password.equals(againpassword)) {
                System.out.println("新密码设置成功");
                u1.setPassWord(password);
                break;
            } else {
                System.out.println("两次输入不一致,请重新输入");
            }
        }
    }//忘记密码方法

    private static void register(ArrayList<User> list1) {

        System.out.println("开始注册：");
        Scanner sc1 = new Scanner(System.in);
        User u1 = new User();//创建对象

        while (true) {
            System.out.println("请输入你的用户名");
            String username = sc1.next();
            //1.先检查->在判断是否唯一
            boolean flag1 = checkUserName(username);
            if (!flag1) {
                continue;
            }
            //2.判断是否唯一
            boolean flag2 = contains(list1, username);
            if (flag2) {
                System.out.println("用户名" + username + "存在,请重新输入");
            } else {
                System.out.println("恭喜您！用户名" + username + "可用");
                u1.setUserName(username);
                break;
            }
        }//用户名判断

        while (true) {
            System.out.println("请设置密码");
            String password = sc1.next();
            System.out.println("请再次输入密码");
            String againPassword = sc1.next();
            if (!password.equals(againPassword)) {
                System.out.println("密码两次不一致请重新输入！");
                continue;
            } else {
                System.out.println("密码设置成功");
                u1.setPassWord(password);
                break;
            }
        }//密码判断

        while (true) {
            System.out.println("请输入注册的身份证号码");
            String personalID = sc1.next();

            boolean flag1 = checkPersonID(personalID);
            if (flag1) {
                System.out.println("身份证号码可用！");
                u1.setPersonalID(personalID);
                break;
            }
        }//身份证号码

        while (true) {
            System.out.println("请输入注册的手机号码");
            String number = sc1.next();

            boolean flag = checkPhoneNumber(number);
            if (flag) {
                System.out.println("手机号码可用");
                u1.setPhoneNumber(number);
                break;
            }
        }//手机号码判断

        list1.add(u1);//添加
        System.out.println("注册成功！");
    }//注册方法


    public static void login(ArrayList<User> list1) {
        for (int i = 0; i < 3; ) {
            System.out.println("请输入你的用户名");
            Scanner sc1 = new Scanner(System.in);
            String uname = sc1.next();
            if (!contains(list1, uname)) {
                System.out.println("用户名未注册，请先注册!");
                return;
            }
            System.out.println("请输入你的密码");
            String password = sc1.next();

            //验证码校验
            while (true) {
                String rightCode = getcode();
                System.out.println("当前验证码是:" + rightCode);
                System.out.println("请输入您的验证码");
                String myCode = sc1.next();

                if (myCode.equalsIgnoreCase(rightCode)) {
                    System.out.println("验证码正确！");
                    break;
                } else {
                    System.out.println("验证码有误,请重新输入!");
                }
            }
            //用户名密码认证
            User u1 = new User(uname, password, null, null);
            boolean flag = checkUserInfo(list1, u1);
            if (flag) {
                System.out.println("登录成功");
                StudentSystem stu1=new StudentSystem();
                stu1.operate();
                break;
            } else {
                if (i == 2) {
                    System.out.println("账号" + uname + "被锁定");
                    return;
                }
                i++;
                System.out.println("登录失败,还有" + (3 - i) + "机会");
            }
        }

    }//登录方法


    //检查用户名
    public static boolean checkUserName(String uname) {
        int len = uname.length();
        if (len < 3 || len > 15) {
            System.out.println("用户名长度必须在3~15位之间");
            return false;
        }
        int count = 0;
        for (int i = 0; i < uname.length(); i++) {
            char c = uname.charAt(i);
            if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
                System.out.println("只能是字母加数字的组合");
                return false;
            }

            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
            }
        }

        if (count <= 0) {
            System.out.println("用户名不能是纯数字");
        }
        return count > 0;
    }

    //检查id
    public static boolean checkPersonID(String personalID) {
        if (personalID.length() != 18) {
            System.out.println("身份证长度不符合要求,请重新输入");
            return false;
        }
        if (personalID.startsWith("0")) {
            System.out.println("身份证不能以0开头");
            return false;
        }
        for (int i = 0; i < personalID.length() - 1; i++) {
            char c = personalID.charAt(i);
            if (c < '0' || c > '9') {
                System.out.println("前17位必须是数字");
                return false;
            }
        }
        char end = personalID.charAt(personalID.length() - 1);
        if (end == 'x' || end == 'X' || (end >= '0' && end <= '9')) {
            return true;
        } else {
            System.out.println("身份证号码最后一位有误!");
            return false;
        }
    }


    //检查手机号码
    public static boolean checkPhoneNumber(String number) {
        if (number.length() != 11) {
            System.out.println("手机号码长度必须为11位");
            return false;
        }
        if (number.startsWith("0")) {
            System.out.println("手机号码不能以0开头");
            return false;
        }
        for (int i = 0; i < number.length(); i++) {
            if (number.charAt(i) < '0' || number.charAt(i) > '9') {
                System.out.println("手机号码必须为纯数字");
                return false;
            }
        }
        return true;
    }

    //获取索引
    public static int getIndex(ArrayList<User> list1, String u_name) {
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).getUserName().equals(u_name))
                return i;
        }

        return -1;
    }


    //是否存在
    public static boolean contains(ArrayList<User> list1, String u_name) {
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).getUserName().equals(u_name)) {
                return true;
            }
        }

        return false;
    }

    //验证码功能（用集合做）开一个test类
    public static String getcode() {//我们可以开一个test类测试
        ArrayList<Character> list1 = new ArrayList<Character>();
        Random r1 = new Random();

        for (int i = 0; i < 26; i++) {
            char c = (char) ('a' + i);
            char C = (char) ('A' + i);
            list1.add(c);
            list1.add(C);
        }


        StringBuilder sb1 = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r1.nextInt(list1.size());
            sb1.append(list1.get(index));
        }
        int number = r1.nextInt(10);
        sb1.append(number);
        char[] arr = sb1.toString().toCharArray();
        for (int i = 0; i < arr.length; i++) {
            int index = r1.nextInt(arr.length);
            char temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }


        return new String(arr);
    }

    //检查用户名密码
    public static boolean checkUserInfo(ArrayList<User> list1, User u1) {
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).getUserName().equals(u1.getUserName()) && list1.get(i).getPassWord().equals(u1.getPassWord())) {
                return true;
            }
        }

        return false;
    }


    //test
    public static void printArr(ArrayList<User> list1) {
        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i).toString());
        }
    }

}
